﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Data.SqlClient;

namespace LxjCommon
{
    public class PageBase:System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            #region 判断用户是否已经登陆
            if (Session["UserModel"] == null)
            {
                //Model.T_User t = new Model.T_User();
                //t.userid = 1;
                //Session["UserModel"]=new Model.T_User();
                // Page.Response.Redirect("~/Login.aspx");
            }
            else
            {

            }
            #endregion
        }
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="LM"></param>
        public void SaveLog(LogModel LM)
        {
            try
            {
                /*建表脚本
 CREATE TABLE [dbo].[LxjCommon_Log](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[LogType] [varchar](50) NULL,
	[LogTitle] [varchar](500) NULL,
	[LogSource] [varchar](8000) NULL,
	[LogUserID] [int] NULL,
	[LogUserTrueName] [varchar](500) NULL,
	[LogContent] [varchar](8000) NULL,
	[LogCreateTime] [datetime] NULL CONSTRAINT [DF_LxjCommon_Log_LogCreateTime]  DEFAULT (getdate())
) ON [PRIMARY]                   
                 */

                string sql = @"INSERT INTO [LxjCommon_Log]
           ([LogType]
           ,[LogTitle]
           ,[LogSource]
           ,[LogUserID]
           ,[LogUserTrueName]
           ,[LogContent]
           ,[LogCreateTime])
     VALUES
           (@LogType
           ,@LogTitle
           ,@LogSource
           ,@LogUserID
           ,@LogUserTrueName
           ,@LogContent
           ,getdate())";
                #region 设置参数
                StackTrace ss = new StackTrace(true);
                Type t = ss.GetFrame(1).GetMethod().DeclaringType;
                string MethodName = ss.GetFrame(1).GetMethod().Name.ToString();
                LM.LogSource = t.FullName + "." + MethodName;
                if (GetUserSession() != null)
                {
                    LM.LogUserID = GetUserSession().UserId.ToString();
                    LM.LogUserTrueName = GetUserSession().UserName;
                }
                else
                {
                    LM.LogUserID = "-1";
                    LM.LogUserTrueName = "0";
                }
                #endregion

                SqlParameter[] SqlParm = new SqlParameter[]{
                new SqlParameter("@LogType", LM.LogType.ToString()),
                new SqlParameter("@LogTitle", LM.LogTitle.ToString()),
                new SqlParameter("@LogSource", LM.LogSource.ToString()),
                new SqlParameter("@LogUserID", LM.LogUserID.ToString()),
                new SqlParameter("@LogUserTrueName", LM.LogUserTrueName.ToString()),
                new SqlParameter("@LogContent", LM.LogContent.ToString()),
                };
                SqlHelper.ExecuteSql(sql, SqlParm);
            }
            catch
            {

            }
        }
        public T_User GetUserSession()
        {
            if (Session["UserModel"] == null)
            {
                return null;
            }
            else
            {
                //设置T_User的id和username
                return (T_User)Session["UserModel"];
            }

        }
        /// <summary>
        /// 编辑试卷用的session
        /// </summary>
        /// <returns></returns>
        //public Model.Paper GetPaper()
        //{
        //    //if (Session["Paper"] == null)
        //    //{
        //    //    Session["Paper"] = new Model.Paper();
        //    //}
        //    //return (Model.Paper)Session["Paper"];
        //}

        #region 页面的异常时间抓取
        protected override void OnError(EventArgs e)
        {
            base.OnError(e);
            Exception ex = Server.GetLastError();
            SaveLog(new LogModel(LogModel.EnumLogType.警告, "Page未处理异常", "Page页未处理异常\r\n" + ex.Message + ex.StackTrace));
        }
        #endregion

        #region
        public void BaseShowMsg(string msg)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + msg + "')</script>");
        }
        #endregion

    }
    /// <summary>
    /// 日志Model
    /// </summary>
    public class LogModel
    {
        /// <summary>
        /// 操作用户真实姓名
        /// </summary>
        public string LogUserTrueName = "";
        private string _LogContent = "";
        public LogModel(EnumLogType LogType, String LogTitle, String LogContent)
        {
            this.LogType = LogType;
            this.LogTitle = LogTitle;
            this.LogContent = LogContent;

        }
        public LogModel(String LogTitle, String LogContent)
        {
            this.LogTitle = LogTitle;
            this.LogContent = LogContent;
        }
        /// <summary>
        /// 日志内容
        /// </summary>
        public string LogContent = "";
        /// <summary>
        /// 用户ID
        /// </summary>
        public string LogUserID="";

        /// <summary>
        /// 日志源，记录调用记录日志的方法的类名称
        /// </summary>
        public string LogSource = "";
        /// <summary>
        /// 标题
        /// </summary>
        public string LogTitle="";

        /// <summary>
        /// 日志类型
        /// </summary>
        public EnumLogType LogType=EnumLogType.信息;

        public enum EnumLogType
        {
            信息 = 0,
            警告 = 1,
            错误 = 2,
        }
    }

    public class T_User
    {
        public int UserId = 0;
        public string UserName = "";
    }
}
